<?php
class ControllerLocalisationOrderStatus extends AdminController {

  	public function index() {

		M('localisation/order_status');

    	$this->getList();
  	}

  	public function insert() {

		M('localisation/order_status');

		if ($this->request->isPost() && $this->validateForm()) {
      		$this->model_localisation_order_status->addOrderStatus($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/order_status'));
		}

    	$this->getForm();
  	}

  	public function update() {

		M('localisation/order_status');

    	if ($this->request->isPost() && $this->validateForm()) {
	  		$this->model_localisation_order_status->editOrderStatus($this->request->get['order_status_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/order_status'));
    	}

    	$this->getForm();
  	}

  	public function delete() {

		M('localisation/order_status');

    	if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $order_status_id) {
				$this->model_localisation_order_status->deleteOrderStatus($order_status_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
      		$this->redirect(UA('localisation/order_status'));
   		}

    	$this->getList();
  	}

  	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
		$this->document->addStyle('view/stylesheet/jquery.minicolors.css');
		
		$qf = new Query_filter();

		$order_status_total = $this->model_localisation_order_status->getTotalOrderStatuses();
		$this->data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses($qf);

		$this->data['pagination'] = $this->pagination($order_status_total)->render();

		$this->display('localisation/order_status_list.tpl');
  	}

  	protected function getForm() {
     	$this->document->setTitle($this->_['heading_title']);
		$this->document->addStyle('view/stylesheet/jquery.minicolors.css');
		
		if (isset($this->request->get['order_status_id'])) {
			$this->data['action'] = UA('localisation/order_status/update', 'order_status_id=' . $this->request->get['order_status_id']);
			if (!$this->request->isPost()) {
				$this->vo->data['order_status'] = $this->model_localisation_order_status->getOrderStatusDescriptions($this->request->get['order_status_id']);
			}
		} else {
			$this->data['action'] = UA('localisation/order_status/insert');
		}

		$this->data['languages'] = C('cache_language_2');

		$this->display('localisation/order_status_form.tpl');
  	}

	protected function validateForm() {
		$pass = true;
    	foreach ($this->request->post['order_status'] as $language_id => $value) {
      		if (!range_length($value['name'], 3, 32)) {
        		$this->setMessage('error_name_' . $language_id, $this->_['error_name']);
				$pass = false;
      		}
    	}

  		return $pass;
  	}

  	protected function validateDelete() {

		M('setting/store');
		M('sale/order');

		foreach ($this->request->post['selected'] as $order_status_id) {
    		if (C('config_order_status_id') == $order_status_id) {
	  			$this->setMessage('error_warning', $this->_['error_default']);
				return false;
			}

    		if (C('config_download_status_id') == $order_status_id) {
	  			$this->setMessage('error_warning', $this->_['error_download']);
				return false;
			}

			$store_total = $this->model_setting_store->getTotalStoresByOrderStatusId($order_status_id);

			if ($store_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_store'], $store_total));
				return false;
			}

			$order_total = $this->model_sale_order->getTotalOrderHistoriesByOrderStatusId($order_status_id);

			if ($order_total) {
	  			$this->setMessage('error_warning', sprintf($this->_['error_order'], $order_total));
				return false;
			}
	  	}

		return true;
  	}
}
?>